import {Component, OnInit} from '@angular/core';
import {FetchService} from "../../../../core/net/fetch.service";
import {DomSanitizer, SafeResourceUrl} from "@angular/platform-browser";
import {format} from "date-fns";

@Component({
  selector: 'app-report-jrsq',
  templateUrl: './jrsq.component.html',
  styleUrls: ['../report.component.scss', './jrsq.component.scss']
})
export class JrsqComponent implements OnInit {
  /*dateValue = new Date();
  totalPlanFlow = 0;
  totalFlow = 0;

  qdsqTable1Data = [];
  qdsqTable2Data = [];
  qdsqTable3Data = [];
  hhsqTableData = [
    {
      a1: '', a2: '', a3: '', b1: '', b2: '', b3: '',
      c1: '', c2: '', d1: '', d2: '',
      e1: '', e2: '', f1: '', f2: '',  h: ''
    }
  ];
  weatherData = [];

  constructor(private fetchService: FetchService) {
  }

  ngOnInit() {
    this.queryInfo();
  }

  queryInfo(): void {
    this.fetchService.post<any>('report/water/list', {"date": this.dateValue.getTime()} ).subscribe(res => {
      this.getChannelTableData(res);
      this.getHhsqTableData(res);
      this.getWeatherData(res);
    });
  }

  // 渠道水情
  getChannelTableData(res): void {
    const chData = res.channel.data;
    this.totalFlow = res.channel.totalFlow;
    this.totalPlanFlow = res.channel.totalPlanFlow;
    this.qdsqTable1Data = chData.slice(0, 7);
    this.qdsqTable2Data = chData.slice(7, 14);
    this.qdsqTable3Data = chData.slice(14, 21);
  }

  // 黄河水情
  getHhsqTableData(res): void {
    const hh = res.huanghe;
    hh.map(item => {
      // 龙羊峡 LYX_RES a
      // 刘家峡 LJX_RES b
      // 兰州 LZ_RIV c
      // 下河沿 XHY_RIV d
      // 青铜峡 QTX_RES e
      // 石嘴山 SZS_RIV f
      if (item.stadiaCode == 'LYX_RES') {
        this.hhsqTableData[0].a1 = item.fluxIn;
        this.hhsqTableData[0].a2 = item.fluxOut;
        this.hhsqTableData[0].a3 = item.height;
      }
      if (item.stadiaCode == 'LJX_RES') {
        this.hhsqTableData[0].b1 = item.fluxIn;
        this.hhsqTableData[0].b2 = item.fluxOut;
        this.hhsqTableData[0].b3 = item.height;
      }
      if (item.stadiaCode == 'LZ_RIV') {
        this.hhsqTableData[0].c1 = item.flux;
        this.hhsqTableData[0].c2 = item.fluxLast;
      }
      if (item.stadiaCode == 'XHY_RIV') {
        this.hhsqTableData[0].d1 = item.flux;
        this.hhsqTableData[0].d2 = item.fluxLast;
      }
      if (item.stadiaCode == 'QTX_RES') {
        this.hhsqTableData[0].e1 = item.flux;
        this.hhsqTableData[0].e2 = item.fluxLast;
      }
      if (item.stadiaCode == 'SZS_RIV') {
        this.hhsqTableData[0].f1 = item.flux;
        this.hhsqTableData[0].f2 = item.fluxLast;
      }
      this.hhsqTableData[0].h = res.huanghehsl;
    });
  }

  getWeatherData(res): void {
    this.weatherData = [];
    res.weather.map(item => {
      this.weatherData.push(
        {
          area: item.area,
          info: item.info,
          wind: item.wind,
          highTemp: item.hignLower.split('/')[0],
          lowTemp: item.hignLower.split('/')[1],
        }
      );
    });
  }*/
  yearValue = 2020;
  monthPickerValue = new Date().getMonth();
  serverUrl = (window as any).environment.reportUrl;
  iframeSrc: SafeResourceUrl;
  datePicker = new Date();

  constructor(
    private fetchService: FetchService,
    private sanitizer: DomSanitizer
  ) {
  }

  ngOnInit() {
    this.queryInfo();
  }

  queryInfo(): void {

    const token = localStorage.token;
    const date = format(this.datePicker, 'YYYY-MM-DD');
    const link = `${this.serverUrl}preview?_u=wrs:今日水情.ureport.xml&_t=3,6&date=${date}&token=${token}`;
    this.iframeSrc = this.sanitizer.bypassSecurityTrustResourceUrl(link);
  }

  dateChange(): void {
    this.queryInfo();
  }
}

